package com.feichuang.chineseWoodCarving.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期处理工具类.
 * 
 * @version v1.0
 */
public class DateUtil {
    /**
     * . patterns
     */
    private static String patterns = "yyyy-MM-dd HH:mm:ss";

    /**
     * . pattens
     */
    private static String patterns2 = "yyyyMMdd";

    /**
     * .
     * 
     * TODO 添加方法注释.
     * 
     * @param date
     *            date
     * @param pattern
     *            p
     * @return str
     */
    public static String dateToString(Date date, String pattern) {
        String temp = "";
        if (pattern != null && !"".equals(pattern)) {
            temp = pattern;
        } else {
            temp = patterns;
        }
        SimpleDateFormat df = new SimpleDateFormat(temp);
        try {
            String str = df.format(date);
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * .
     * 
     * TODO 添加方法注释.
     * 
     * @param date
     *            d
     * @param pattern
     *            p
     * @return date
     */
    public static Date stringToDate(String date, String pattern) {
        String temp = "";
        if (pattern != null && !"".equals(pattern)) {
            temp = pattern;
        } else {
            temp = patterns2;
        }
        SimpleDateFormat format = new SimpleDateFormat(temp);
        try {
            Date tempDate = format.parse(date);
            return tempDate;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}
